<?php
declare(strict_types=1);

namespace Service\Consumer\YunCangConsumer\Router;
use Service\BaseService;
use App\Model\BbcModel\GoodsBrand;
use Service\Code\Normal;

/**
 * 品牌变更
 * @author chenshubo
 */
class brandChange extends BaseService
{
    /**
     * 接收消费数据
     * @param int $sid
     * @param int $cid
     * @param array $data
     * @param string $messageId
     * @param int $IsFinished
     */
    public function consumer(int $sid,int $cid,array $data,string $messageId,int $IsFinished)
    {
        try 
        {
            $before = $data['before'];
            $after  = $data['after'];
            $brand  = GoodsBrand::instance()->info(['name'=>$before['BrandName'],'cid'=>$cid]);
            if( empty($brand) ) 
            {
                $newBrand= GoodsBrand::instance()->info(['name'=>$after['BrandName'],'cid'=>$cid]);
                if( empty($newBrand) )
                {
                    $insert = [];
                    $insert['name']               = $after['BrandName'];
                    $insert['cid']                = $cid;
                    $insert['sid']                = $sid;
                    $insert['brand_first_letter'] = $after['BrandFirstLetter']??'';
                    $insert['banner_image']       = $after['BannerImage']??'';
                    $insert['pic']                = $after['BrandImage']??'';
                    $insert['cd']                 = time();
                    GoodsBrand::instance()->add($insert);
                }
                else
                {
                    if( $newBrand['df'] == 1 )
                    {
                        GoodsBrand::instance()->where('id',$newBrand['id'])->update([
                            'brand_first_letter'    => $after['BrandFirstLetter']??'',
                            'banner_image'          => $after['BannerImage']??'',
                            'pic'                   => $after['BrandImage']??'',
                            'df'                    => 0
                        ]);
                    }
                }
            }
            else
            {
                GoodsBrand::instance()->where('id',$brand['id'])->update([
                    'name'                  => $after['BrandName'],
                    'brand_first_letter'    => $after['BrandFirstLetter']??'',
                    'banner_image'          => $after['BannerImage']??'',
                    'pic'                   => $after['BrandImage']??'',
                    'df'                    => 0
                ]);

            }
            return Normal::returnCode(Normal::SUC,"处理完成");
        } catch ( \Throwable $th ) {
            $error = [
                'error'         => $th->getMessage(),
                'errorFile'     => $th->getFile(),
                'errorLine'     => $th->getLine(),
            ];
            $_class_name = __CLASS__;
            return Normal::returnCode(Normal::FAIL,"{$_class_name}消费处理处理异常【{$error['error']}】",$error);
        }
    }
}